/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.security.bean;

import java.util.Comparator;

import org.springframework.security.GrantedAuthority;

/**
 * {@link Comparator} for {@link GrantedAuthority}s.
 */
public class GrantedAuthorityComparator
    implements Comparator<GrantedAuthority>
{

  /**
   * the unique instance of the comparator.
   */
  public static final GrantedAuthorityComparator INSTANCE = new GrantedAuthorityComparator();



  /**
   * private constructor.
   */
  private GrantedAuthorityComparator()
  {
    // Do nothing.
  }



  /**
   * Compares its two arguments for order. Returns a negative integer, zero, or
   * a positive integer as the first argument is less than, equal to, or greater
   * than the second.
   * <p>
   * 
   * @param ga1
   *          the first object to be compared.
   * @param ga2
   *          the second object to be compared.
   * @return a negative integer, zero, or a positive integer as the first
   *         argument is less than, equal to, or greater than the second.
   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
   */
  public int compare(GrantedAuthority ga1, GrantedAuthority ga2)
  {
    if (ga1.getAuthority() == ga2.getAuthority()) {
      return 0;
    }
    if (ga1.getAuthority() == null) {
      return 1;
    }
    if (ga2.getAuthority() == null) {
      return -1;
    }
    return ga1.getAuthority().compareTo(ga2.getAuthority());
  }

}
